package com.ycy.temperature.view.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

import com.jude.utils.JUtils;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.ycy.temperature.R;
import com.ycy.temperature.bean.VerificationBean;
import com.ycy.temperature.date.net.UserNetWorks;
import com.ycy.temperature.date.utils.CheckUtils;
import com.ycy.temperature.date.utils.DownTimer;
import com.ycy.temperature.view.base.BaseActivity;
import com.ycy.temperature.view.view.TitleView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by MI on 2018/7/17.
 */

public class ForgetActivity extends BaseActivity {
    @BindView(R.id.title)
    TitleView title;
    @BindView(R.id.Text_Usermobile)
    AutoCompleteTextView TextUsermobile;
    @BindView(R.id.Text_VerifyCode)
    AutoCompleteTextView TextVerifyCode;
    @BindView(R.id.Text_Verification)
    TextView TextVerification;
    @BindView(R.id.Text_Password)
    AutoCompleteTextView TextPassword;
    @BindView(R.id.Text_TwePassword)
    AutoCompleteTextView TextTwePassword;
    @BindView(R.id.Text_Register)
    TextView TextRegister;

    @Override
    protected int getLayout() {
        return R.layout.activity_forget;
    }

    @Override
    protected void setData() {
        TextVerification.setClickable(false);
        TextVerification.setEnabled(false);
        TextVerification.setBackgroundResource(R.color.bg_LightGray);
        TextRegister.setClickable(false);
        TextRegister.setEnabled(false);
        TextRegister.setBackgroundResource(R.color.bg_LightGray);
    }

    @Override
    protected void init(Bundle inBundle) {
        ButterKnife.bind(this);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
        title.getLeft_ibtn().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextUsermobile.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(final CharSequence charSequence, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                if (charSequence.length() < 11) {
                    TextVerification.setClickable(false);
                    TextVerification.setEnabled(false);
                    TextVerification.setBackgroundResource(R.color.bg_LightGray);
                    TextRegister.setClickable(false);
                    TextRegister.setEnabled(false);
                    TextRegister.setBackgroundResource(R.color.bg_LightGray);

                } else {
                    if (!isChinaPhoneLegal(TextUsermobile.getText().toString())) {
                        TextVerification.setClickable(false);
                        TextVerification.setEnabled(false);
                        TextVerification.setBackgroundResource(R.color.bg_LightGray);
                        TextRegister.setClickable(false);
                        TextRegister.setEnabled(false);
                        TextRegister.setBackgroundResource(R.color.bg_LightGray);
                    } else {
                        TextVerification.setClickable(true);
                        TextVerification.setEnabled(true);
                        TextVerification.setBackgroundResource(R.color.bg_Blue);
                        TextRegister.setClickable(true);
                        TextRegister.setEnabled(true);
                        TextRegister.setBackgroundResource(R.color.bg_Blue);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    @OnClick({R.id.Text_Verification, R.id.Text_Register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.Text_Verification:
                getVerification();
                break;
            case R.id.Text_Register:
                if (CheckUtils.isEmpty(TextVerifyCode.getText().toString())) {
                    JUtils.Toast("请输入验证码");
                    return;
                }
                if (CheckUtils.isEmpty(TextPassword.getText().toString())) {
                    JUtils.Toast("请输入密码");
                    return;
                }
                if (CheckUtils.isEmpty(TextTwePassword.getText().toString())) {
                    JUtils.Toast("请输入再次密码");
                    return;
                }
                if (TextPassword.getText().toString().equals(TextTwePassword.getText().toString())) {
                    getRegister();
                } else {
                    JUtils.Toast("两次密码不同");
                }

                break;
        }
    }

    public void getRegister() {
        UserNetWorks.getRegister(TextUsermobile.getText().toString(), TextVerifyCode.getText().toString(), TextTwePassword.getText().toString(), "1", new Subscriber<VerificationBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                JUtils.Toast("网络错误");
            }

            @Override
            public void onNext(VerificationBean verificationBean) {
                if (verificationBean.getResponseState().equals("1")) {
                    JUtils.Toast(verificationBean.getMsg());
                    finish();
                } else {
                    JUtils.Toast(verificationBean.getMsg());
                }
            }
        });
    }

    public void getVerification() {
        UserNetWorks.getVerification(TextUsermobile.getText().toString(), "1", new Subscriber<VerificationBean>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                JUtils.Toast("网络错误");
            }

            @Override
            public void onNext(VerificationBean verificationBean) {
                if (verificationBean.getResponseState().equals("1")) {
                    JUtils.Toast(verificationBean.getMsg());
                    getButton();
                } else {
                    JUtils.Toast(verificationBean.getMsg());
                }
            }
        });
    }

    public static int Interval = 1000;
    DownTimer timer;

    public void getButton() {
        TextVerification.setEnabled(false);
        timer = new DownTimer();
        timer.setTotalTime(60 * Interval);
        timer.setIntervalTime(Interval);
        timer.setTimerLiener(new DownTimer.TimeListener() {
            @Override
            public void onFinish() {
                TextVerification.setEnabled(true);
                TextVerification.setText("获取验证码");
            }

            @Override
            public void onInterval(long remainTime) {
                TextVerification.setText(remainTime / 1000
                        + "秒后" + "再试");
            }
        });
        timer.start();
    }

    public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
        String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }
}
